using System;
using Cysharp.Threading.Tasks;
using Entity.Player;
using MoonFramework.FSM;
using Tool.Name;
using UnityEngine;
using Random = UnityEngine.Random;

namespace Entity.Monster.State
{
    public class SpiderIdleState : MonsterIdleState
    {
        private bool _isIdle;
        public override void Enter()
        {
            _isIdle = true;
            base.Enter();
        }

        protected override async UniTask Idle()
        {
            while (_isIdle)
            {
                PlayAudio().Forget(); //异步挂起随机喊叫
                await UniTask.Yield();
            }
        }
        
        private async UniTaskVoid PlayAudio()
        {
            await UniTask.Delay(TimeSpan.FromSeconds(Random.Range(0, 6)));
            if (Random.Range(0, 30) == 0) _actor.viewController.PlayAudio(MonsterAudioName.Idle, 0.5f);
        }

        public override void Exit()
        {
            base.Exit();
            _isIdle = false;
            _actor.viewController.targetDir = PlayerController.Instance.Player.viewController.transform.position;
            Monster.MonsterObject.NavMeshAgent.enabled = true;
            Monster.MonsterObject.NavMeshAgent.SetDestination(_actor.viewController.targetDir);   
        }
    }
}